package com.nageoffer.shortlink.project.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nageoffer.shortlink.project.dao.entity.LinkOsStatsDo;
import com.nageoffer.shortlink.project.dto.req.ShortLinkStatsReqDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.HashMap;
import java.util.List;

/**
 * 操作系统访问持久层
 */
public interface LinkOsStatsMapper extends BaseMapper<LinkOsStatsDo> {

    @Insert("Insert Into t_link_os_stats (gid, full_short_url, date, cnt,os,create_time, update_time, del_flag)values " +
            "(#{linkOsStats.gid},#{linkOsStats.fullShortUrl},#{linkOsStats.date},#{linkOsStats.cnt}" +
            ",#{linkOsStats.os},now(),now(),0) " +
            "on DUPLICATE KEY UPDATE cnt = cnt +#{linkOsStats.cnt}")
    void shortLinkOsStats(@Param("linkOsStats") LinkOsStatsDo linkOsStatsDo);


    /**
     * 根据短链接获取指定日期内操作系统监控数据
     */
    @Select("SELECT " +
            "    os, " +
            "    SUM(cnt) AS count " +
            "FROM " +
            "    t_link_os_stats " +
            "WHERE " +
            "    full_short_url = #{param.fullShortUrl} " +
            "    AND gid = #{param.gid} " +
            "    AND date BETWEEN #{param.startDate} and #{param.endDate} " +
            "GROUP BY " +
            "    full_short_url, gid, date, os;")
    List<HashMap<String, Object>> listOsStatsByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);

}